package com.mo.modular.search.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TopDocs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.mo.common.lucene.LuceneQueryManager;
import com.mo.common.lucene.interfaceAction.LuceneReader;
import com.mo.common.lucene.utils.DocAndBeanUtils;
import com.mo.common.lucene.utils.LuceneClient;
import com.mo.search.model.MixedCont;
import com.mo.search.service.ISearchMixedService;

/**
 * 
 *
 * @author chenxiaodong
 * @date 2018-08-23 12:34
 */
@Service
public class SearchMixedServiceImpl implements ISearchMixedService {

	@Value("${path.lucenePath}")
	private String lucenePath;
	private LuceneQueryManager luceneQueryManager;

	@Override
	public List<MixedCont> queryMixedContListByKeyword(String keyword) throws Exception {
		luceneQueryManager = new LuceneQueryManager(new LuceneClient(lucenePath));
		LuceneReader<List<MixedCont>> reader = new LuceneReader<List<MixedCont>>() {
			@SuppressWarnings("unchecked")
			@Override
			public List<MixedCont> read( IndexSearcher indexSearcher, TopDocs topDocs)
					throws Exception {
				return (List<MixedCont>) DocAndBeanUtils
						.toBeanList(indexSearcher, topDocs.scoreDocs, MixedCont.class);
			}
		};
		final Map<String,List<MixedCont>> datas = new HashMap<String,List<MixedCont>>();
		Thread t1 =new Thread(){
			public void run() {
				//进行精确查找
				List<MixedCont> av1 = new ArrayList<MixedCont>();;
				try {
					av1 = luceneQueryManager.readForTermQuery(reader, "contName", keyword);
					datas.put("first",av1);
				} catch (Exception e) {
					e.printStackTrace();
				}
			};
		};
		Thread t2 =new Thread(){
			public void run() {
				//进行模糊查找--通配符查找
				List<MixedCont> av2 = new ArrayList<MixedCont>();
				try {
					av2 = luceneQueryManager.queryTermParserTest(reader, "contName", "*"+keyword+"*");
					datas.put("second",av2);
				} catch (Exception e) {
					e.printStackTrace();
				}
			};
		};
		/*Thread t3 =new Thread(){
			public void run() {
				//推荐关联字查找--
				List<MixedCont> av3 = new ArrayList<MixedCont>();;
				try {
					av3 = luceneQueryManager.queryParserTest(reader, "description", keyword);
					datas.put("third",av3);
				} catch (Exception e) {
					e.printStackTrace();
				}
			};
		};*/
		
		t1.start();
		t2.start();
//		t3.start();
		t1.join();
		t2.join();
//		t3.join();
		List<MixedCont> mixedContList=new LinkedList<MixedCont>();
		List<MixedCont> av1 = datas.get("first");
		av1:for(int j=0;j<av1.size();j++){
			MixedCont mc = av1.get(j);
			for(int m=0;m<mixedContList.size();m++){
				if(mixedContList.get(m).getContName().equals(mc.getContName())
						&& mixedContList.get(m).getId().equals(mc.getId())
						&& mixedContList.get(m).getContType().equals(mc.getContType())){
					continue av1;
				}
			}
			mixedContList.add(mc);
		}
		
		List<MixedCont> av2 = datas.get("second");
		av2:for(int j=0;j<av2.size();j++){
			MixedCont mc = av2.get(j);
			for(int m=0;m<mixedContList.size();m++){
				if(mixedContList.get(m).getContName().equals(mc.getContName())
						&& mixedContList.get(m).getId().equals(mc.getId())
						&& mixedContList.get(m).getContType().equals(mc.getContType())){
					continue av2;
				}
			}
			mixedContList.add(mc);
		}
		
		/*List<MixedCont> av3 = datas.get("third");
		av3:for(int j=0;j<av3.size();j++){
			MixedCont mc = av3.get(j);
			for(int m=0;m<mixedContList.size();m++){
				if(mixedContList.get(m).getContName().equals(mc.getContName())
						&& mixedContList.get(m).getId().equals(mc.getId())
						&& mixedContList.get(m).getContType().equals(mc.getContType())){
					continue av3;
				}
			}
			mixedContList.add(mc);
		}*/
		
		return mixedContList;
	}
		
}
